package cc.comac.mom.datahandlers.entity;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cc.comac.mom.bean.EquipRepairReplacePart;
import cc.comac.mom.bean.EquipSparePartsDue;
import cc.comac.mom.bean.ResultDev;
import cc.comac.mom.dao.entity.EquipDevAreaDefDao;
import cc.comac.mom.dao.entity.EquipRepairDao;
import cc.comac.mom.dao.entity.EquipRepairReplacePartDao;
import cc.comac.mom.dao.entity.EquipSparePartMasterDataDao;
import cc.comac.mom.entity.EquipDevAreaDefEntity;
import cc.comac.mom.entity.EquipPartBorrowDemandEntity;
import cc.comac.mom.entity.EquipRepairEntity;
import cc.comac.mom.entity.EquipRepairReplacePartEntity;
import cc.comac.mom.entity.EquipSparePartMasterDataEntity;
import cc.comac.mom.entity.EquipSparePartsDueEntity;
import cc.comac.mom.util.BaseEntityDataUtil;
import cc.comac.mom.util.ResultBean;
import cc.comac.mom.util.TimeUtil;

@Component
@Transactional
public class EquipRepairReplacePartDataHandler extends ServiceImpl<EquipRepairReplacePartDao, EquipRepairReplacePartEntity> {

	@Autowired
	private EquipRepairReplacePartDao equipRepairReplacePartDao;
	@Autowired
	private BaseEntityDataUtil baseEntityDataUtil;
	
	@Autowired
	private EquipPartBorrowDemandDataHandler equipPartBorrowDemandDataHandler;
	
	@Autowired
	private EquipSparePartMasterDataDao equipSparePartMasterDataDao;
	
	@Autowired
	private EquipRepairDao equipRepairDao;
	
	@Autowired
	private EquipSparePartsDueDataHandler equipSparePartsDueDataHandler;
	@Autowired
	private EquipDevAreaDefDao equipDevAreaDefDao;
	
	public ResultBean insertBatch(List<EquipRepairReplacePart> list) {
		ResultBean resultBean = new ResultBean(true);
		if(list == null || list.size()<1) {
			throw new RuntimeException("数据为空");
		}
		ArrayList<EquipRepairReplacePartEntity> listEntity = new ArrayList<EquipRepairReplacePartEntity>();
		for(EquipRepairReplacePart item :list) {
			EquipRepairReplacePartEntity innerData = item.getInnerData();
			if(innerData == null) {
				throw new RuntimeException("数据为空");
			}
			innerData.setReplaceStatus("0");
			listEntity.add(innerData);
		}
		for(EquipRepairReplacePartEntity item :listEntity) {
			item.setReplaceStatus("0");
			baseEntityDataUtil.setBaseEntityData(item);
		}
		boolean saveBatch = this.saveBatch(listEntity);
		if(!saveBatch) {
			resultBean.setErrorMessge("保存更换备件信息失败");
			return resultBean;
		}
		return resultBean;
	}
	//修改
	public ResultBean update(EquipRepairReplacePart obj) {
		ResultBean resultBean = new ResultBean(true);
		if(obj == null || obj.getInnerData() == null){
			throw new RuntimeException("数据为空");
		}
		EquipRepairReplacePartEntity innerData = obj.getInnerData();
		EquipRepairReplacePartEntity selectById = equipRepairReplacePartDao.selectById(obj.getInnerData().getId());
		if(selectById == null) {
			throw new RuntimeException("修改数据不存在");
		}
		if(selectById.getSubmitReceive() == 1) {
			throw new RuntimeException("该信息已提交需求确认，不可修改");
		}
		baseEntityDataUtil.updateBaseEntityData(innerData);
		int updateById = equipRepairReplacePartDao.updateById(innerData);
		if(updateById<1) {
			throw new RuntimeException("修改更换备件信息失败");
		}
		return resultBean;
	}
	//删除
	public ResultBean delete(EquipRepairReplacePart obj) {
		ResultBean resultBean = new ResultBean(true);
		if(obj == null || obj.getInnerData() == null){
			throw new RuntimeException("数据为空");
		}
		EquipRepairReplacePartEntity selectById = equipRepairReplacePartDao.selectById(obj.getInnerData().getId());
		if(selectById == null) {
			throw new RuntimeException("删除数据不存在");
		}
		if(selectById.getSubmitReceive() == 1) {
			throw new RuntimeException("该信息已提交需求确认，不可删除");
		}
		baseEntityDataUtil.deleteBaseEntityData(selectById);
		int updateById = equipRepairReplacePartDao.updateById(selectById);
		if(updateById<1) {
			throw new RuntimeException("删除更换备件信息失败");
		}
		return resultBean;
	}
	
	//批量删除
	public ResultBean deleteBatch(List<Long> ids) {
		ResultBean resultBean = new ResultBean(true);
		if(ids == null || ids.size()<1) {
			throw new RuntimeException("数据为空");	
		}
		Collection<EquipRepairReplacePartEntity> listByIds = this.listByIds(ids);
		if(listByIds == null || listByIds.size()<1) {
			throw new RuntimeException("数据为空");
		}
		for(EquipRepairReplacePartEntity item:listByIds) {
			if(item.getSubmitReceive() == 1) {
				throw new RuntimeException("该信息已提交需求确认，不可删除");
			}
			baseEntityDataUtil.deleteBaseEntityData(item);
		}
		boolean updateBatchById = this.updateBatchById(listByIds);
		if(!updateBatchById) {
			throw new RuntimeException("删除更换备件信息失败");
		}
		return resultBean;
	}
	
	//查询
	public ResultDev selectByCondition(EquipRepairReplacePart obj) {
		QueryWrapper<EquipRepairReplacePartEntity> queryWrapper = new QueryWrapper<EquipRepairReplacePartEntity>();
		queryWrapper.eq("row_status", 1);
		queryWrapper.ne("replace_status", "2");
		if (obj.getPageSize() == 0) {
			obj.setPageSize(10);
		}
		if (obj != null && obj.getInnerData() != null) {
			if (obj.getInnerData().getPartNumber() != null && !"".equals(obj.getInnerData().getPartNumber())) {
				queryWrapper.like("part_number", obj.getInnerData().getPartNumber());
			}
			if (obj.getInnerData().getPartName() != null && !"".equals(obj.getInnerData().getPartName())) {
				queryWrapper.like("part_name", obj.getInnerData().getPartName());
			}	
			if (obj.getInnerData().getSparePartModel() != null && !"".equals(obj.getInnerData().getSparePartModel())) {
				queryWrapper.like("spare_part_model", obj.getInnerData().getSparePartModel());
			}			
			if (obj.getInnerData().getRepairId() != 0) {
				queryWrapper.eq("repair_id", obj.getInnerData().getRepairId());
			}
		}
		Page<EquipRepairReplacePartEntity> page = new Page<>(obj.getPageNum(), obj.getPageSize(), true);
		IPage<EquipRepairReplacePartEntity> iPage = equipRepairReplacePartDao.selectPage(page, queryWrapper);
		List<EquipRepairReplacePartEntity> records = iPage.getRecords();
		if (records != null && records.size() > 0) {
			ArrayList<EquipRepairReplacePart> listEquipRepairPart= new ArrayList<EquipRepairReplacePart>();
			for (EquipRepairReplacePartEntity item : records) {
				EquipRepairReplacePart equipRepairPart = new EquipRepairReplacePart();
				equipRepairPart.setInnerData(item);
				listEquipRepairPart.add(equipRepairPart);
			}
			ResultDev resultDev = new ResultDev();
			resultDev.setEntityList(listEquipRepairPart);
			resultDev.setTotalQuantity((int) iPage.getTotal());
			resultDev.setPageCount((int) iPage.getPages());
			return resultDev;

		}
		return null;

	}

	// 批量需求确认
	public ResultBean repairReplacePartDemandConfirm(List<EquipRepairReplacePart> repairReplacePartList) {
		ResultBean resultBean = new ResultBean();
		if (repairReplacePartList.size() > 0) {
			ArrayList<EquipPartBorrowDemandEntity> entityList = new ArrayList<EquipPartBorrowDemandEntity>();
			List<Long> ids = new ArrayList<Long>();
			for (EquipRepairReplacePart equipRepairReplacePart : repairReplacePartList) {
				EquipRepairReplacePartEntity innerData = equipRepairReplacePart.getInnerData();
				// 查询对应备件表数据
				ids.add(innerData.getId());
				QueryWrapper<EquipSparePartMasterDataEntity> queryWrapper = new QueryWrapper<EquipSparePartMasterDataEntity>();
				queryWrapper.eq("row_status", 1);
				queryWrapper.eq("spare_part_id", innerData.getPartId());
				EquipSparePartMasterDataEntity selectOne = equipSparePartMasterDataDao.selectOne(queryWrapper);
				// 查询对应维修数据
				QueryWrapper<EquipRepairEntity> queryWrapper2 = new QueryWrapper<EquipRepairEntity>();
				queryWrapper2.eq("row_status", 1);
				queryWrapper2.eq("repair_info_id", innerData.getRepairId());
				EquipRepairEntity repairInfoEntity = equipRepairDao.selectOne(queryWrapper2);
				
				EquipPartBorrowDemandEntity equipPartBorrowDemandEntity = new EquipPartBorrowDemandEntity();
				equipPartBorrowDemandEntity.setApplyQuantity(innerData.getReplaceQuantity());
				equipPartBorrowDemandEntity.setApplyTime(TimeUtil.getLocalDate());
				equipPartBorrowDemandEntity.setApplyUser(baseEntityDataUtil.getCurrentUser());
				equipPartBorrowDemandEntity.setBorrowDemandStatus("1");
				equipPartBorrowDemandEntity.setBorrowStyle("1");
				equipPartBorrowDemandEntity.setDevName(repairInfoEntity.getDevName());
				equipPartBorrowDemandEntity.setDevNumber(repairInfoEntity.getDevNumber());
				equipPartBorrowDemandEntity.setJobNumber(baseEntityDataUtil.getJobNumber());
				equipPartBorrowDemandEntity.setRelationNumber(repairInfoEntity.getPlanNumber());
				equipPartBorrowDemandEntity.setSparePartId(innerData.getPartId());
				equipPartBorrowDemandEntity.setSparePartModel(selectOne.getSparePartModel());
				equipPartBorrowDemandEntity.setSparePartName(selectOne.getSparePartName());
				equipPartBorrowDemandEntity.setSparePartNumber(selectOne.getSparePartNumber());
				equipPartBorrowDemandEntity.setUserDepartment(repairInfoEntity.getUserDepartment());
				baseEntityDataUtil.setBaseEntityData(equipPartBorrowDemandEntity);
				entityList.add(equipPartBorrowDemandEntity);
			}
			// 批量保存需求确认数据
			equipPartBorrowDemandDataHandler.saveBatch(entityList, entityList.size());
			
			// 批量查询维修备件更换
			List<EquipRepairReplacePartEntity> selectBatchIds = equipRepairReplacePartDao.selectBatchIds(ids);
			for (EquipRepairReplacePartEntity equipRepairReplacePartEntity : selectBatchIds) {
				equipRepairReplacePartEntity.setSubmitTime(TimeUtil.getLocalDate());
				equipRepairReplacePartEntity.setSubmitReceive(1);
			}
			this.updateBatchById(selectBatchIds, selectBatchIds.size());
		}
		return resultBean;
	}

	// 更换确认
	public ResultBean repairReplacePartReplaceConfirm(List<EquipRepairReplacePart> repairReplacePartList) {
		ResultBean resultBean = new ResultBean(true);
		List<EquipRepairReplacePartEntity> entityList = new ArrayList<EquipRepairReplacePartEntity>();
		List<EquipSparePartsDue> PartsDueList = new ArrayList<EquipSparePartsDue>();
		if (repairReplacePartList.size() > 0) {
			for (EquipRepairReplacePart equipRepairReplacePart : repairReplacePartList) {
				EquipRepairReplacePartEntity innerData = equipRepairReplacePart.getInnerData();
				// 查询备件替换数据
				EquipRepairReplacePartEntity selectById2 = equipRepairReplacePartDao.selectById(innerData.getId());
				selectById2.setReplaceTime(TimeUtil.getLocalDate());
				selectById2.setReplaceStatus("1");
				baseEntityDataUtil.updateBaseEntityData(selectById2);
				entityList.add(selectById2);
				EquipSparePartsDueEntity equipSparePartsDueEntity = new EquipSparePartsDueEntity();
				// 查询对应维修数据
				QueryWrapper<EquipRepairEntity> queryWrapper2 = new QueryWrapper<EquipRepairEntity>();
				queryWrapper2.eq("row_status", 1);
				queryWrapper2.eq("repair_info_id", innerData.getRepairId());
				EquipRepairEntity repairInfoEntity = equipRepairDao.selectOne(queryWrapper2);
				// 查询对应备件数据
				QueryWrapper<EquipSparePartMasterDataEntity> queryWrapper = new QueryWrapper<EquipSparePartMasterDataEntity>();
				queryWrapper.eq("row_status", 1);
				queryWrapper.eq("spare_part_id", innerData.getPartId());
				EquipSparePartMasterDataEntity selectOne = equipSparePartMasterDataDao.selectOne(queryWrapper);
				// 通过部位主键查询部位数据
				EquipDevAreaDefEntity selectById = equipDevAreaDefDao.selectById(innerData.getEquipPartAreaId());
				equipSparePartsDueEntity.setBenchmarkDevPart(selectById.getAreaNumber());
				equipSparePartsDueEntity.setDevName(repairInfoEntity.getDevName());
				equipSparePartsDueEntity.setDevNumber(repairInfoEntity.getDevNumber());
				equipSparePartsDueEntity.setMaterialType(selectOne.getSparePartType());
//				equipSparePartsDueEntity.setPartName(selectOne.getSparePartName());
				equipSparePartsDueEntity.setPartNumber(selectOne.getSparePartNumber());
				equipSparePartsDueEntity.setReplacementNumber(innerData.getReplaceQuantity().intValue());
				equipSparePartsDueEntity.setReplacementTime(TimeUtil.getLocalDateTime());
				equipSparePartsDueEntity.setReplacementType("1");
				equipSparePartsDueEntity.setSpecifications(selectOne.getSparePartModel());
//				equipSparePartsDueEntity.setUnit(selectOne.getSparePartsUnit());
				equipSparePartsDueEntity.setWorkNumber(repairInfoEntity.getPlanNumber());
				EquipSparePartsDue equipSparePartsDue = new EquipSparePartsDue();
				equipSparePartsDue.setInnerData(equipSparePartsDueEntity);
				PartsDueList.add(equipSparePartsDue);
			}
			// 更新备件更换数据
			this.updateBatchById(entityList, entityList.size());
			// 更新到期查询数据
			equipSparePartsDueDataHandler.save(PartsDueList);
		}
		return resultBean;
	}
}
